﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Web;
namespace HZ.DrugTrade.BLL
{
    public class BLL_UserSession
    {

        private static Dictionary<string, HZ.DrugTrade.Model.User> cache1 = new Dictionary<string, HZ.DrugTrade.Model.User>(1024);
        private static Dictionary<string, DateTime> cache2 = new Dictionary<string, DateTime>(1024);




        #region C/S系统的Session机制
        /// <summary>
        /// 获取当前活动用户
        /// </summary>
        /// <returns>用户实体</returns>
        public static HZ.DrugTrade.Model.User GetCurrentUser()
        {
            if (CallContext.GetData("X-SessionID") != null)
            {
                string clientIp = CallContext.GetData("ClientIPAddress").ToString();
                string sessionid = CallContext.GetData("X-SessionID").ToString();
                if (cache1.ContainsKey(sessionid))
                {

                    cache2[sessionid] = DateTime.Now;
                    return cache1[sessionid];
                }
                else
                {
                    return null;
                }
            }
            else if (HttpContext.Current != null)
            {
                string sessionid = HttpContext.Current.Session.SessionID;
                if (cache1.ContainsKey(sessionid))
                {
                    cache2[sessionid] = DateTime.Now;
                    return cache1[sessionid];
                }
                else
                {
                    return null;
                }
            }
            return null;

        }

        public static void CheckSession()
        {
#if DEBUG
            if (BLL_UserSession.GetCurrentUser() == null)
            {
                throw new Exception("当前调用未经授权");

            }
#endif
        }

        public static void Logoff()
        {
            if (CallContext.HostContext != null && CallContext.GetData("X-SessionID") != null)
            {
                string clientIp = CallContext.GetData("ClientIPAddress").ToString();
                string sessionid = CallContext.GetData("X-SessionID").ToString();

                cache1.Remove(sessionid);
                cache2.Remove(sessionid);

            }
            else if (HttpContext.Current != null)
            {
                string sessionid = HttpContext.Current.Session.SessionID;
                cache1.Remove(sessionid);
                cache2.Remove(sessionid);
            }
        }
        /// <summary>
        /// 绑定上下文用户
        /// </summary>
        /// <param name="usermodel"></param>
        public static void SetCurrentUser(HZ.DrugTrade.Model.User usermodel)
        {
            if (CallContext.GetData("X-SessionID") != null)
            {
                string sessionid = CallContext.GetData("X-SessionID").ToString();
                cache1[sessionid] = usermodel;
                cache2[sessionid] = DateTime.Now;

                // return;
            }
            else if (HttpContext.Current != null)
            {
                string sessionid = HttpContext.Current.Session.SessionID;
                cache1[sessionid] = usermodel;
                cache2[sessionid] = DateTime.Now;
            }

        }
        /// <summary>
        /// 刷新用户活动缓存
        /// </summary>
        public static void RefreshCache()
        {
            string sessionid = null;
            if (CallContext.GetData("X-SessionID") != null)
            {
                sessionid = CallContext.GetData("X-SessionID").ToString();
            }
            else if (HttpContext.Current != null)
            {
                sessionid = HttpContext.Current.Session.SessionID;
            }
            cache2[sessionid] = DateTime.Now;
        }
        #endregion

        #region C/S系统的缓存过期机制

        static BLL_UserSession()
        {
            System.Threading.Timer tm = new System.Threading.Timer(new System.Threading.TimerCallback(PastDue));
            tm.Change(3600, 86400);
        }
        private static void PastDue(object state)
        {
            System.Threading.Timer timer = (System.Threading.Timer)state;
            // 对缓存进行刷新 过期时间24小时

            IList<string> deletekeys = new List<string>();
            foreach (var item in cache1.Keys)
            {
                if (cache2[item].AddDays(1) < DateTime.Now)
                {
                    deletekeys.Add(item);

                }

            }

            foreach (var item in deletekeys)
            {
                cache2.Remove(item);
                cache1.Remove(item);
            }

        }
        #endregion
    }
}
